/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.HangSanXuatDAO;
import dao.SanPhamDAO;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.CuaHang;
import pojo.HangSanXuat;
import pojo.SanPham;

/**
 *
 * @author LaiHuynh
 */
public class TimKiemSanPham extends javax.swing.JInternalFrame {

    private DefaultComboBoxModel dcbm;
    private DefaultTableModel dtm;
    private DefaultComboBoxModel cbModel;

    /**
     * Creates new form TimKiemSanPham
     */
    private void hienThiDanhSach() {
        //doc du lieu
        this.dtm = new DefaultTableModel();
        List<SanPham> ds = SanPhamDAO.layDanhSachSanPham();
        this.dtm.addColumn("Ma San Pham");
        this.dtm.addColumn("Ten San Pham");
        this.dtm.addColumn("Gia Ban");
        this.dtm.addColumn("So Luong Ton");
        this.dtm.addColumn("Hang SX");
        this.dtm.addColumn("Cua Hang");
        for (int i = 0; i < ds.size(); i++) {
            SanPham sp = ds.get(i);
            Vector vt1 = new Vector();
            vt1.add(sp.getMaSanPham());
            vt1.add(sp.getTenSanPham());
            vt1.add(sp.getGiaBan());
            vt1.add(sp.getSoLuongTon());
            HangSanXuat hsx = sp.getHangSanXuat();
            CuaHang ch = sp.getCuaHang();
            vt1.add(hsx.getTenHang());
            vt1.add(ch.getTenCuaHang());
            this.dtm.addRow(vt1);
        }
        this.jtDanhSach.setModel(dtm);
    }

    public TimKiemSanPham() {
        initComponents();
        this.dcbm = new DefaultComboBoxModel();
        this.jcbbHangSX.setModel(dcbm);
        this.dtm = new DefaultTableModel();
        Gia();
        SoLuong();
    }
    public void Gia(){
        this.cbModel=new DefaultComboBoxModel();
        this.jcbgia.setModel(cbModel);
        Vector vt=new Vector();
        vt.add(0, "chọn");
        this.cbModel.addElement(vt);
        this.cbModel.addElement("dưới 1000000");
        this.cbModel.addElement("từ 100000 đến 5000000");
        this.cbModel.addElement("trên 500000");
    }
     public void SoLuong(){
        this.cbModel=new DefaultComboBoxModel();
        this.jcbsoluong.setModel(cbModel);
        
        this.cbModel.addElement("[chọn]");
        
        this.cbModel.addElement("<100");
        this.cbModel.addElement("từ 100 đến 500");
        this.cbModel.addElement("trên 500");
    }
    public void timKiem() {
        try {
            this.dtm = new DefaultTableModel();
            Vector vt = (Vector) this.jcbbHangSX.getSelectedItem();
            int maHangsx = (Integer) vt.get(0);
            String tenSanPham = this.jtfTenSanPham.getText();
            int gia=this.jcbgia.getSelectedIndex();
            int soluong=this.jcbsoluong.getSelectedIndex();
                    
            List<SanPham> ds = SanPhamDAO.timKiemSanPham(tenSanPham, maHangsx,gia,soluong);
            this.dtm.addColumn("Ma San Pham");
            this.dtm.addColumn("Ten San Pham");
            this.dtm.addColumn("Gia Ban");
            this.dtm.addColumn("So Luong Ton");
            this.dtm.addColumn("Hang SX");
            this.dtm.addColumn("Cua Hang");
            for (int i = 0; i < ds.size(); i++) {
                SanPham sp = ds.get(i);
                Vector vt1 = new Vector();
                vt1.add(sp.getMaSanPham());
                vt1.add(sp.getTenSanPham());
                vt1.add(sp.getGiaBan());
                vt1.add(sp.getSoLuongTon());
                HangSanXuat hsx = sp.getHangSanXuat();
                CuaHang ch = sp.getCuaHang();
                vt1.add(hsx.getTenHang());
                vt1.add(ch.getTenCuaHang());
                this.dtm.addRow(vt1);
            }
            this.jtDanhSach.setModel(dtm);
        } catch (Exception ex) {
            //JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDanhSach = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtfTenSanPham = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jcbbHangSX = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jcbgia = new javax.swing.JComboBox();
        jcbsoluong = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jbtThoat = new javax.swing.JButton();
        excel = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("TÌM KIẾM SẢN PHẨM");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin sản phẩm"));

        jtDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtDanhSach.setEnabled(false);
        jScrollPane1.setViewportView(jtDanhSach);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 585, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách sản phẩm theo thông tin"));

        jLabel1.setText("Tên sản phẩm:");

        jtfTenSanPham.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfTenSanPhamCaretUpdate(evt);
            }
        });
        jtfTenSanPham.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfTenSanPhamActionPerformed(evt);
            }
        });

        jLabel2.setText("Hãng sản xuất:");

        jcbbHangSX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbbHangSXActionPerformed(evt);
            }
        });

        jLabel3.setText("Đơn Giá");

        jcbgia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbgia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbgiaActionPerformed(evt);
            }
        });

        jcbsoluong.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbsoluong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbsoluongActionPerformed(evt);
            }
        });

        jLabel4.setText("Số Lượng ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jtfTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jcbbHangSX, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(45, 45, 45)
                        .addComponent(jLabel2)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbgia, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbsoluong, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbbHangSX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbgia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbsoluong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        excel.setText("Xuất File Excel");
        excel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(excel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(excel)
                .addGap(18, 18, 18)
                .addComponent(jbtThoat)
                .addGap(270, 270, 270))
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        try {
            List<HangSanXuat> ds = HangSanXuatDAO.layDanhSachHangSanXuat();
            HangSanXuat hsx=null;
            Vector vt = new Vector();
            vt.add(0);
            vt.add("chon");
             dcbm.addElement(vt);
            for (int i = 0; i < ds.size(); i++) {
                hsx = ds.get(i);
                vt=new Vector();
                vt.add(hsx.getMaHangsx());
                vt.add(hsx.getTenHang());
                dcbm.addElement(vt);
            }
            this.hienThiDanhSach();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_formInternalFrameOpened

    private void jcbbHangSXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbbHangSXActionPerformed
        // TODO add your handling code here:
        if(this.jcbbHangSX.getSelectedIndex()!=0){
        this.timKiem();
        }
    }//GEN-LAST:event_jcbbHangSXActionPerformed

    private void jtfTenSanPhamCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfTenSanPhamCaretUpdate
        // TODO add your handling code here:
        this.timKiem();
    }//GEN-LAST:event_jtfTenSanPhamCaretUpdate

    private void jtfTenSanPhamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfTenSanPhamActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfTenSanPhamActionPerformed

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void jcbgiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbgiaActionPerformed
        // TODO add your handling code here:
        //            LaySP();
        //            HangSX();
        //            SoLuong();
        int index=this.jcbgia.getSelectedIndex();
        if(index !=0){
        this.timKiem();
        //JOptionPane.showMessageDialog(this, index);
        }
    }//GEN-LAST:event_jcbgiaActionPerformed

    private void jcbsoluongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbsoluongActionPerformed
        // TODO add your handling code here:
        this.timKiem();
    }//GEN-LAST:event_jcbsoluongActionPerformed

    private void excelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excelActionPerformed
        // TODO add your handling code here:
        XuatExcel xuatexcel=new XuatExcel();
        JFileChooser chooser=new JFileChooser();
        chooser.showSaveDialog(this);
        String filename = chooser.getSelectedFile().getPath();
         File file = new File(filename+".XLS"); 
        
        try {
            xuatexcel.XuatFile(this.jtDanhSach, file);
        } catch (IOException ex) {
            Logger.getLogger(TimKiemSanPham.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
        
    }//GEN-LAST:event_excelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton excel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JComboBox jcbbHangSX;
    private javax.swing.JComboBox jcbgia;
    private javax.swing.JComboBox jcbsoluong;
    private javax.swing.JTable jtDanhSach;
    private javax.swing.JTextField jtfTenSanPham;
    // End of variables declaration//GEN-END:variables
}
